import {
    batchDeleteByIdPrice, deleteByIdPrice, findAll, findPageprice, insert, updateByIdPrice, upload,updateByIdPricePass,updateByIdPriceNoPass} from '@/api/goods/price'
import pickerOptions from '@/utils/date';
import {findPage} from '@/api/goods/goods';
import {brandFindAll}from '@/api/goods/brand';
import {findlistColour}from '@/api/goods/colour'
import {findlistType}from '@/api/goods/type';
// import base64 from "@/utils/base64Utils";
import showMessageBox from '@/utils/showMessageBox';


export default {
    data() {
        return {
            pickerOptions:pickerOptions,
            tableData:[],
            tableData1:[],
            id:0,
            total:0,
            total1:0,
            batchIds:[],
            searchParams:{currentPage:1,pageSize:5},//价格单查询条件
            searchParams1:{
                currentPage:1,
                pageSize:5,brandId:"",colourId:""},//商品查询条件
            dialogFormVisible:false,
            dialogFormVisible1:false,
            dialogFormVisible2:false,
            formData:{},
            formData1:{},
            imageUrl:'',
            options: [{
                value: '未审核',
                label: '未审核'
            }, {
                value: '已审核',
                label: '已审核'
            }, {
                value: '审核不通过',
                label: '审核不通过'
            }],
            brandList:{},//品牌集合
            typeList:{},//类型集合
            colourList:{},//颜色集合
        };
    },
    methods:{
        upload(formData){
            upload(formData).then(r => {
                this.imageUrl = r;
                this.formData.brandLogo = r;
            });
        },

        chooseTime(){
            this.searchParams.startTime = this.pickerOptions.startDate[0];
            this.searchParams.endTime = this.pickerOptions.startDate[1];
            console.log("chooseTime");
        },
        async saveOrUpdate(){
            if (this.formData.id) {
                await this.updateById(this.formData.id)
            }else {
                await this.save(this.formData);
            }
            //  console.log("saveOrUpdate()");
            // await insert(this.formData);
            this.searchPage();
        },
        async updateById(){
            let a = await updateByIdPrice(this.formData);
            if (a){
                this.$message({
                    message: '修改成功',
                    type: 'success'
                });
            }else {
                this.$message.error('修改失败');
            }
        },
        async save(){
            console.log(this.formData.teffectiveTime);
            let a = await insert(this.formData);
            if (a){
                this.$message({
                    message: '添加成功',
                    type: 'success'
                });
            }else {
                this.$message.error('添加失败');
            }
        },
        async findById(row){
            console.log(row)
            // this.formData = await row;
            this.formData.goodsId=row.goodsId;
            this.formData.goodsName=row.goodsName;
            this.formData.goodsStock=row.minimumStock;
            this.formData.salePrice=row.salePrice;
            this.formData.mallPrice=row.mallPrice;

        },
        async pricefindById(row){
            console.log(row)
            this.formData = await row;
            this.formData1 = await row;
        },
        selectChange(brandList){
            if (brandList.length == 1){
                this.formData.id = brandList[0].id;
            }
            this.batchIds = brandList.map(item=>item.id);
        },

        showMsg(){
            showMessageBox.smb('此操作将永久删除该文件, 是否继续?', '删除成功!', '已取消删除').then(r  =>{
                r ? this.batchDeleteByIds():null
            })
        },
        selectAll(){
            findAll().then(response=>{
                this.tableData =response;
            })
        },

        //async 表示searchPage 是异步的，但是 await 表示 要等到 findPage 执行后，代码才会继续执行
        //分页查条件查询
        async searchPage(){
            let response = await findPageprice(this.searchParams);
                this.tableData = response.data;
                this.total=response.total;
        },
        async goodssearchPage(){
            // this.searchParams.typeId = this.goodstypename[this.goodstypename.length-1];
            let response = await findPage(this.searchParams1);
                console.log(this.searchParams.typeId);
            // console.log(this.goodstypename);
            // console.log(this.goodstypename[this.goodstypename.length-1]);

                this.tableData1 = response.data;
                this.total1=response.total;
        },
        //分页默认值
        resetForm(){
            this.searchParams = {currentPage: 1,pageSize: 5}
            this.pickerOptions.startDate='';
        },
        currentPageChange(currentIndex){
            this.searchParams.currentPage=currentIndex;
            this.searchPage();
        },
        async deleteById(){
            let a = await deleteByIdPrice(this.id);
            if (a){
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
            }else {
                this.$message.error('删除失败');
            }
            await this.searchPage();
        },
        async batchDeleteByIds() {
            await batchDeleteByIdPrice(this.batchIds);
            this.searchPage();
        },
        //查询品牌
        async findbrandList(){
            // console.log("aaa");
            let response = await brandFindAll();
            this.brandList = response;
        },
        //查询颜色
        async findcolourList(){
            let response = await findlistColour();
            // console.log(response);
            this.colourList = response;
        },
        //查询类型
        async findtypeList(){
            let response = await findlistType();
            this.typeList = response;
        }, handleChange(){
        }, productcurrentPageChange(currentIndex){
            this.searchParams1.currentPage=currentIndex;
            this.productPage();
        },
        async productPage(){
            // this.searchParams1.typeId = this.goodstypename[this.goodstypename.length-1];
            let response = await findPage(this.searchParams1);
                this.tableData1 = response.data;
                this.total1=response.total;
        },
        //产品分页默认值
        productresetForm(){
            this.searchParams1 = {currentPage: 1,pageSize: 5}
            this.productPage();
        },
        jisuan(){
            // eslint-disable-next-line no-empty
            if (this.formData.value1){
                //上调
                if (this.formData.value2){
                    //百分比
                    //市场
                    this.formData.adjustmentSalePrice=this.formData.salePrice+this.formData.salePrice*(this.formData.priceAdjustmentRange/100);
                    //商城
                    this.formData.adjustmentMallPrice=this.formData.mallPrice+this.formData.mallPrice*(this.formData.priceAdjustmentRange/100);
                }else {
                    //绝对值
                    this.formData.adjustmentSalePrice=this.formData.salePrice+this.formData.priceAdjustmentRange;
                    this.formData.adjustmentMallPrice=this.formData.mallPrice+this.formData.priceAdjustmentRange;
                }

            }else {
                //下调
                if (this.formData.value2){
                    //百分比
                    //市场
                    this.formData.adjustmentSalePrice=this.formData.salePrice*(this.formData.priceAdjustmentRange/100);
                    //商城
                    this.formData.adjustmentMallPrice=this.formData.mallPrice*(this.formData.priceAdjustmentRange/100);
                }else {
                    //绝对值
                    this.formData.adjustmentSalePrice=this.formData.salePrice-this.formData.priceAdjustmentRange;
                    this.formData.adjustmentMallPrice=this.formData.mallPrice-this.formData.priceAdjustmentRange;
                }
            }
        },
        tiao(){
            if (this.formData.value1){
                //上调
                if (this.formData.value2){
                    //百分比
                    //市场
                    this.formData.adjustmentSalePrice=this.formData.salePrice+this.formData.salePrice*(this.formData.priceAdjustmentRange/100);
                    //商城
                    this.formData.adjustmentMallPrice=this.formData.mallPrice+this.formData.mallPrice*(this.formData.priceAdjustmentRange/100);
                }else {
                    //绝对值
                    this.formData.adjustmentSalePrice=this.formData.salePrice+this.formData.priceAdjustmentRange;
                    this.formData.adjustmentMallPrice=this.formData.mallPrice+this.formData.priceAdjustmentRange;
                }

            }else {
                //下调
                if (this.formData.value2){
                    //百分比
                    //市场
                    this.formData.adjustmentSalePrice=this.formData.salePrice*(this.formData.priceAdjustmentRange/100);
                    //商城
                    this.formData.adjustmentMallPrice=this.formData.mallPrice*(this.formData.priceAdjustmentRange/100);
                }else {
                    //绝对值
                    this.formData.adjustmentSalePrice=this.formData.salePrice-this.formData.priceAdjustmentRange;
                    this.formData.adjustmentMallPrice=this.formData.mallPrice-this.formData.priceAdjustmentRange;
                }
            }
        },
        zhi(){
            var priceAdjustmentRange = this.formData.priceAdjustmentRange;
            if (this.formData.value1){
                //上调
                if (!this.formData.value2){

                    //绝对值
                    this.formData.adjustmentSalePrice=priceAdjustmentRange+this.formData.salePrice;
                    this.formData.adjustmentMallPrice=priceAdjustmentRange+this.formData.mallPrice;

                }else {
                    // var priceAdjustmentRange = this.formData.priceAdjustmentRange;
                    //百分比
                    //市场
                        this.formData.adjustmentSalePrice=this.formData.salePrice+this.formData.salePrice*(priceAdjustmentRange/100);
                    //商城
                        this.formData.adjustmentMallPrice=this.formData.mallPrice+this.formData.mallPrice*(priceAdjustmentRange/100);

                }

            }else {
                //下调
                if (!this.formData.value2){
                    // var priceAdjustmentRange = this.formData.priceAdjustmentRange;
                    //绝对值
                    this.formData.adjustmentSalePrice=this.formData.salePrice-priceAdjustmentRange;
                    this.formData.adjustmentMallPrice=this.formData.mallPrice-priceAdjustmentRange;

                }else {
                    // var priceAdjustmentRange = this.formData.priceAdjustmentRange;
                     //百分比
                    //市场

                    this.formData.adjustmentSalePrice=this.formData.salePrice-this.formData.salePrice*(priceAdjustmentRange/100);
                    //商城
                    this.formData.adjustmentMallPrice=this.formData.mallPrice-this.formData.mallPrice*(priceAdjustmentRange/100);

                }
            }
        },
        pass(){
            var _this = this;
            let yy = new Date().getFullYear();
            let mm = new Date().getMonth()+1;
            let dd = new Date().getDate();
            let hh = new Date().getHours();
            let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
            let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
            _this.gettime = yy+'-'+mm+'-'+dd+' '+hh+':'+mf+':'+ss;
            this.formData.auditTime = _this.gettime;
            let a = updateByIdPricePass(this.formData);
            if (a){
                this.$message({
                    message: '审核完成',
                    type: 'success'
                });
                this.searchPage();
            }else {
                this.$message.error('审核失败');
            }

        },
        noPass(){
            var _this = this;
            let yy = new Date().getFullYear();
            let mm = new Date().getMonth()+1;
            let dd = new Date().getDate();
            let hh = new Date().getHours();
            let mf = new Date().getMinutes()<10 ? '0'+new Date().getMinutes() : new Date().getMinutes();
            let ss = new Date().getSeconds()<10 ? '0'+new Date().getSeconds() : new Date().getSeconds();
            _this.gettime = yy+'-'+mm+'-'+dd+' '+hh+':'+mf+':'+ss;
            this.formData.auditTime = _this.gettime;
            let a = updateByIdPriceNoPass(this.formData);
            if (a){
                this.$message({
                    message: '审核完成',
                    type: 'success'
                });
                this.searchPage();
            }else {
                this.$message.error('审核失败');
            }

        }

    },
    created(){
        this.searchPage()
        this.goodssearchPage();
        this.findcolourList();
        this.findbrandList();
        this.findtypeList();
    }
};
